/*
 * lib2Desa - Another 2D game library in C++
 * Copyright (C) 2009 Erivaldo Xavier de Lima Filho
 *
 * This program is free software and is distributed under GNU General Public License version 3. You can modify and/or redistribute
 * it under the terms of GPL version 3 or posterior version. This program comes with NO WARRANTY, for more details, see LICENSE,
 * or <http://www.gnu.org/licenses>.
 *
 * Contact info:
 *
 *	Erivaldo Xavier de Lima Filho, desadoc@gmail.com
 *
 */

#include "SDL/Sprite.hpp"

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"

static SDL_Surface* load_image(const char* filename){

	SDL_Surface* loaded, *optimized;
	loaded=optimized=0;

	loaded = IMG_Load(filename);

	if(loaded){
		optimized = SDL_DisplayFormat(loaded);
		SDL_FreeSurface(loaded);
	}

	return optimized;
}

#ifndef _NO_NAMESPACE
namespace lib2Desa {
#endif

namespace SDL {

class SpriteIn
{
public:
	SDL_Surface* img;
};

Sprite::Sprite(void)
{
	in = new SpriteIn;
	in->img = 0;
}

Sprite::~Sprite(void)
{
	if(in->img)
	{
		SDL_FreeSurface(in->img);
	}

	delete in;
}

bool Sprite::load(const char* filename)
{
	if(in->img)
	{
		SDL_FreeSurface(in->img);
		in->img = 0;
	}

	in->img = load_image(filename);

	if(!in->img) return false;

	return true;
}

void* Sprite::getImageData(void) const
{
	return (void*) in->img;
}

vector<2,unsigned int> Sprite::getSize(void) const
{
	vector<2,unsigned int> size(in->img->w, in->img->h);
	return size;
}

void Sprite::setColorKey(Color key)
{
	Uint32 ck = SDL_MapRGB(in->img->format, key.get_r(), key.get_g(), key.get_b());
	SDL_SetColorKey(in->img, SDL_SRCCOLORKEY, ck);
}

}

#ifndef _NO_NAMESPACE
}
#endif
